//
//  main.c
//  Rexel
//
//  Created by Zach Zahos on 2014-05-18.
//  Copyright (c) 2014 Zach Zahos. All rights reserved.
//

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "RexelHelpers.h"


int main(int argc, const char * argv[])
{
	RXLTokenizer *tokenizer = RXLTokenizerCreate();
	
	RXLTypeDefCharactersSettings *identifierSettings = malloc(sizeof(RXLTypeDefCharactersSettings));
	identifierSettings->validPrefixes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
	identifierSettings->prefixes = strlen(identifierSettings->validPrefixes);
	identifierSettings->validCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
	identifierSettings->characters = strlen(identifierSettings->validCharacters);
	__unused RXLType identifierType = RXLTokenizerAddTypeDef(tokenizer, &RXLTypeDefCharacters, identifierSettings);
	
	RXLTypeDefCharactersSettings *whitespaceSettings = malloc(sizeof(RXLTypeDefCharactersSettings));
	whitespaceSettings->validCharacters = " \t\r\n";
	whitespaceSettings->characters = strlen(whitespaceSettings->validCharacters);
	__unused RXLType whitespaceType = RXLTokenizerAddTypeDef(tokenizer, &RXLTypeDefCharacters, whitespaceSettings);
	
	RXLTypeDefStringQuoteSettings **quotes = malloc(sizeof(RXLTypeDefStringQuoteSettings*) * 3);
	RXLTypeDefStringQuoteSettings *quote = malloc(sizeof(RXLTypeDefStringQuoteSettings));
	quote->prefix = "\"";
	quote->prefixLength = strlen(quote->prefix);
	quote->suffix = "\"";
	quote->suffixLength = strlen(quote->suffix);
	quote->multiline = RXLFalse;
	quotes[0] = quote;
	quote = malloc(sizeof(RXLTypeDefStringQuoteSettings));
	quote->prefix = "'";
	quote->prefixLength = strlen(quote->prefix);
	quote->suffix = "'";
	quote->suffixLength = strlen(quote->suffix);
	quote->multiline = RXLFalse;
	quotes[1] = quote;
	quote = malloc(sizeof(RXLTypeDefStringQuoteSettings));
	quote->prefix = "\"\"\"";
	quote->prefixLength = strlen(quote->prefix);
	quote->suffix = "\"\"\"";
	quote->suffixLength = strlen(quote->suffix);
	quote->multiline = RXLTrue;
	quotes[2] = quote;
	RXLTypeDefStringSettings *stringSettings = malloc(sizeof(RXLTypeDefStringSettings));
	stringSettings->quoteSettings = quotes;
	stringSettings->quotes = 3;
	stringSettings->escapeCharacter = '\\';
	__unused RXLType stringType = RXLTokenizerAddTypeDef(tokenizer, &RXLTypeDefString, stringSettings);

	RXLTypeDefNumberBaseSettings **bases = malloc(sizeof(RXLTypeDefNumberBaseSettings*) * 4);
	RXLTypeDefNumberBaseSettings *base = malloc(sizeof(RXLTypeDefNumberBaseSettings));
	base->base = 'd';
	base->validDigits = "0123456789";
	base->digits = strlen(base->validDigits);
	base->allowDecimal = RXLTrue;
	base->allowExponent = RXLTrue;
	bases[0] = base;
	base = malloc(sizeof(RXLTypeDefNumberBaseSettings));
	base->base = 'b';
	base->validDigits = "01";
	base->digits = strlen(base->validDigits);
	base->allowDecimal = RXLFalse;
	base->allowExponent = RXLFalse;
	bases[1] = base;
	base = malloc(sizeof(RXLTypeDefNumberBaseSettings));
	base->base = 'o';
	base->validDigits = "012345678";
	base->digits = strlen(base->validDigits);
	base->allowDecimal = RXLFalse;
	base->allowExponent = RXLFalse;
	bases[2] = base;
	base = malloc(sizeof(RXLTypeDefNumberBaseSettings));
	base->base = 'x';
	base->validDigits = "0123456789abcdefABCDEF";
	base->digits = strlen(base->validDigits);
	base->allowDecimal = RXLFalse;
	base->allowExponent = RXLFalse;
	bases[3] = base;
	RXLTypeDefNumberSettings *numberSettings = malloc(sizeof(RXLTypeDefNumberSettings));
	numberSettings->allowPositive = RXLFalse;
	numberSettings->allowNegative = RXLTrue;
	numberSettings->validBases = bases;
	numberSettings->bases = 4;
	__unused RXLType numberType = RXLTokenizerAddTypeDef(tokenizer, &RXLTypeDefNumber, numberSettings);
	
	char **names = malloc(sizeof(char *) * ((long)numberType + 3));
	names[RXLTypeUnknown+2] = "unknown";
	names[RXLTypeEOF+2] = "eof";
	names[identifierType+2] = "identifier";
	names[whitespaceType+2] = "whitespace";
	names[stringType+2] = "string";
	names[numberType+2] = "number";
	
	char *text = "test1 -1.2 \"s t\\\n r's\" 0b01010101 'test\\'s' 0o12345678 \"\"\"new\n line\"\"\" test2";
	RXLTokenizeRequest *request = RXLTokenizeRequestCreateBuffer(text, strlen(text));
	RXLToken *token = NULL;
	do {
		token = RXLTokenizerNextToken(tokenizer, request);
		printf("Token %s (%ld):\n", names[token->type+2], token->type);
		if (token->token && token->length) {
			printf("%s\n\n", token->token);
		} else {
			printf("\n\n");
		}
	} while (token->type != RXLTypeEOF);

    return 0;
}

